package org.kukker.app;

import org.kukker.R;
import org.kukker.SmsIntentReceiver;
import org.kukker.activity.BrowseSavedEventsActivity;
import org.kukker.activity.SearchEventsActivity;
import org.kukker.upcoming.UpcomingLocalService;


import android.app.Application;
import android.app.ApplicationContext;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentReceiver;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.telephony.PhoneStateIntentReceiver;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.SubMenu;
import android.view.Menu.Item;
import android.widget.Button;
import android.widget.Toast;

public class KukkerApp extends Application {
	public static IntentReceiver smsIntentReceiver;
    public static IntentFilter smsIntentFilter;
    public static Handler smsIntentHandler;
    public static Handler callIntentHandler;    
    public static PhoneStateIntentReceiver phoneStateIntentReceiver;    
    public static DisplayMetrics mDisplayMetrics;
    
    public static UpcomingLocalService mUpcomingLocalService;
    private boolean isConnectedUpcomingService = false;
   
    protected final String APIKEY_GOOGLEMAPS = "ABQIAAAAtaOuKYhhcJpcZVhPzlKn2xQzQpWkeMfLFWWIQx2SioaiJckJ4xRAtF5iFq6epS5dl1NgV-LWjCcJyw"; 
    protected final String APIKEY_UPCOMING = "6efc1a7606";
    
    public final static String NAMESPACE_ANDROID ="http://schemas.android.com/apk/res/android";
    		
 //   public static DisplayMetrics displayMetrics = new android.util.DisplayMetrics();
    private Thread mThread1; 
    
    private ServiceConnection mUpcomingLocalServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {    
        	UpcomingLocalService.LocalBinder upcomingBinder = (UpcomingLocalService.LocalBinder)service;
        	//upcomingBinder.invokeWebService(uws);
        	
            mUpcomingLocalService = upcomingBinder.getService();
            
            android.os.Message msg = mUpcomingLocalService.mServiceHandler.obtainMessage();
            
            msg.callback = new Runnable(){            	
    			public void run() {
    				Log.i("service2","callback");    				
    			}    			
    		};
	    	msg.what = 1;
	    	mUpcomingLocalService.mServiceHandler.dispatchMessage(msg);
            
            Toast.makeText(KukkerApp.this, "Connected to Kukker2 service",
            Toast.LENGTH_SHORT).show();
            isConnectedUpcomingService = true;
            
            
            /*
            mThread1=
            new Thread(
           			new Runnable(){
    						@Override
    						public void run() {
    							while(true){
    								try {
										Thread.sleep(100);
										//Log.i("KukkerApp","[Thread1]");
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
    								mUpcomingLocalService.test();
    							}
    						}
           			}
           	);
            mThread1.start();
            */
    			
        }
        public void onServiceDisconnected(ComponentName className) {          
            mUpcomingLocalService = null;
            Toast.makeText(KukkerApp.this, "Disconnected to Upcoming service",
                  Toast.LENGTH_SHORT).show();
            
            isConnectedUpcomingService = false;
        }
    };
    
    public void onCreate(){    	 
    	
	//	Log.i("lifecycle",this.getClass().getName()+", onCreate");	
		
		KukkerApp.smsIntentReceiver = new SmsIntentReceiver();
		//KukkerApp.smsIntentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");	    
		KukkerApp.smsIntentHandler = new Handler();    
	  
	  //  Log.i("service2",this.getClass().getName()+"Creating service");		
		ComponentName cname = startService(new Intent(KukkerApp.this,  UpcomingLocalService.class), null);
		if(cname != null){
			String fCName = cname.flattenToString();
		//	Log.i("KukkerApp",fCName);
		}else{
			//Log.i("KukkerApp","null");
		}		
		
		boolean isServiceConnected = false;
		isServiceConnected = bindService(new Intent(KukkerApp.this,UpcomingLocalService.class), mUpcomingLocalServiceConnection, Context.BIND_AUTO_CREATE);
		 
		//Log.i("service2",this.getClass().getName()+" service connected: "+isServiceConnected);		
		
		
		
	/*
	    try {
	    	
	    	
			phoneStateIntentReceiver = PhoneStateIntentReceiver.class.newInstance();
		    callIntentHandler = new CallIntentHandler(phoneStateIntentReceiver);		    
		    phoneStateIntentReceiver.setContext(this);
		    phoneStateIntentReceiver.setTarget(callIntentHandler);
		    
		    
		    this.registerReceiver(smsIntentReceiver,smsIntentFilter, null, smsIntentHandler);
		    
		    
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		
		phoneStateIntentReceiver.registerIntent();		
		 */
		 
		 
	}
	public void onTerminate(){
		/*
		Log.i("lifecycle",this.getClass().getName()+", onTerminate");
		*/
        stopService(new Intent(KukkerApp.this,
                UpcomingLocalService.class));
	}
	
	
	public static final int ACTIVITY_EVENTSEARCH = 1;
	public static final int ACTIVITY_EVENTCREATE = 2;
	
	
	
	
	public static class Icons{
		public static final int OPTIONS_HELP =  R.drawable.icon_question_orange;
		public static final int OPTIONS_EVENT = R.drawable.icon_calendar_orange;
		public static final int OPTIONS_SEARCH = R.drawable.icon_search_orange;
		public static final int OPTIONS_OPTIONS = R.drawable.icon_tool_orange;
		public static final int OPTIONS_SEARCH_START = R.drawable.icon_start_red_32x32;
		public static final int OPTIONS_SEARCH_PAUSE = R.drawable.icon_wait_red_32x32;
		
		public static final int OPTIONS_EVENT_SHOWDETAILS = R.drawable.icon_zoomin_red_32x32;
		public static final int OPTIONS_EVENT_CREATENEW = R.drawable.icon_plus_red_32x32;
		public static final int OPTIONS_EVENT_SENDTOFRIENDS = R.drawable.icon_user_red_32x32;
		public static final int OPTIONS_EVENT_DELETE = R.drawable.icon_trash_red_32x32;
		public static final int OPTIONS_EVENT_SAVE = R.drawable.icon_save_red_32x32;
		public static final int OPTIONS_EVENT_EDIT = R.drawable.icon_write3_red_32x32;

		
		
		public static final int OPTIONS_SEARCH_PUBLIC = R.drawable.icon_globe_red_32x32;
		public static final int OPTIONS_SEARCH_LOCAL = R.drawable.icon_mobile_red_32x32;
		
		public static final int OPTIONS_HELP_ABOUT = R.drawable.kukkericon;
		public static final int OPTIONS_HELP_TIPS = R.drawable.icon_info2_red_32x32;
		
		public static final int OPTIONS_PREFERENCES = R.drawable.icon_tool_red_32x32;
		
	}
	
	public static class Labels{
		public static String event = "";
		public static String help = "";
		public static String search = "";
		public static String options = "";
		public static String showeventdetails = "";
		public static String createnew = "";
		public static String sendtofriends = "";
		public static String delete = "";
		public static String searchpublicevents = "";
		public static String searchsavedevents = "";
		public static String about = "";
		public static String tips = "";
		public static String preferences = "";			
	}
	
	public static class MonthNames{
		public static String January = "January";
		public static String February = "February";
		public static String March = "March";
		public static String April = "April";
		public static String May = "May";
		public static String June = "June";
		public static String July = "July";
		public static String August = "August";
		public static String September = "September";
		public static String October = "October";
		public static String November = "November";
		public static String December = "December";		
	}
	
	
	public static String getMonthName(int num){
		//num is 1-starting index.
		
		if(num==1){
			return MonthNames.January;
		}
		if(num==2){
			return MonthNames.February;
		}

		if(num==3){
			return MonthNames.March;
		}

		if(num==4){
			return MonthNames.April;
		}

		if(num==5){
			return MonthNames.May;
		}

		if(num==6){
			return MonthNames.June;
		}

		if(num==7){
			return MonthNames.July;
		}

		if(num==8){
			return MonthNames.August;
		}

		if(num==9){
			return MonthNames.September;
		}

		if(num==10){
			return MonthNames.October;
		}

		if(num==11){
			return MonthNames.November;
		}

		if(num==12){
			return MonthNames.December;
		}
		
		return ""+num;	
	}
	
	public static class OptionsMenu{
		// context menu options
		public static final int CMO_EVENT_SAVETOLOCAL = 40;
		public static final int CMO_EVENT_REMOVEFROMLOCAL = 41;
		public static final int CMO_EVENT_FORWARD = 60;
		public static final int CMO_EVENT_CANCEL = 3;
		public static final int CMO_EVENT_SHOWDETAILS = 70;
		public static final int CMO_EVENT_EDIT = 51;
		
		
		// options menu options
		public static final int OMO_SEARCH = 30;
		public static final int OMO_SEARCH_PAUSE = 301;
		public static final int OMO_SEARCH_RESUME = 302;
		public static final int OMO_SEARCHPUBLICEVENTS = 31;
		public static final int OMO_SEARCHSAVEDEVENTS = 32;
		public static final int OMO_SAVETOLOCAL = 40;
		public static final int OMO_CREATEEVENT = 50;
		public static final int OMO_EDITEVENT = 51;
		public static final int OMO_DELETEFROMLOCAL = 41;
		public static final int OMO_SENDTOFRIENDS = 60;
		public static final int OMO_SHOWDETAILS = 70;
		
		public static final int OMO_ABOUT = 100;
		
		public static final int OMO_PREFERENCES = 80;
		public static final int OMO_TIPS = 90;
	}
	
	public static class OptionsMenuLabel{
		// context menu options
		public static final String CMO_EVENT_SAVETOLOCAL = "Save to device";
		public static final String CMO_EVENT_REMOVEFROMLOCAL = "Remove from device";
		public static final String CMO_EVENT_FORWARD = "Send to friends";
		public static final String CMO_EVENT_CANCEL = "Cancel";
		public static final String CMO_EVENT_SHOWDETAILS = "Show details";
		public static final String CMO_EVENT_EDIT = "Edit details";
		
		// options menu options
		public static final String OMO_SEARCH = "Search";
		public static final String OMO_ABOUT = "About";
		public static final String CMO_EDITEVENT = "Edit details";
		public static final String OMO_SEARCH_PAUSE = "Pause search";
		public static final String OMO_SEARCH_RESUME = "Resume search";
		public static final String OMO_SEARCHPUBLICEVENTS = "Public events";
		public static final String OMO_SEARCHSAVEDEVENTS = "Saved events";
		public static final String OMO_SAVETOLOCAL = "Save to device";
		public static final String OMO_CREATEEVENT = "Create event";
		public static final String OMO_DELETEFROMLOCAL = "Delete from device";
		public static final String OMO_SENDTOFRIENDS = "Send to friends";
		public static final String OMO_SHOWDETAILS = "Show details";
		
		public static final String OMO_PREFERENCES = "Preferences";
		public static final String OMO_TIPS = "Tips";
	}
	
	public static String constructDateString(int year,int month,int day){

		//construct date string
		String startDate = "";
		String yearStr="";
		yearStr += year + "-";
		//Log.i("debug","formatted date: "+formatted);
		String monthStr = "";
		if(month< 10){
			monthStr += "0"+month+"-";
			
		}else{
			monthStr += month+"-";
		}
		String dateStr = "";
		if(day < 10){
			dateStr += "0"+day;
		}else{
			dateStr += day;
		}
		startDate += yearStr+monthStr+dateStr;
		return startDate;		
	}
	
	public static int[] decodeDateString(String dateString){
		int ret[] = new int[3];
		ret[0] = Integer.parseInt(dateString.substring(0,4));
		ret[1] = Integer.parseInt(dateString.substring(5,7));
		ret[2] = Integer.parseInt(dateString.substring(8));
		return ret;
	}
	
	public static void createOptionsMenu(Menu menu){
		//Log.i("optionsmenu",this.getClass().getName()+", onCreateOptionsMenu");
		menu.setQwertyMode(false);
		
		SubMenu subMenu1 = menu.addSubMenu(0, 0, "Event",KukkerApp.Icons.OPTIONS_EVENT);
		subMenu1.setDefaultItem(KukkerApp.OptionsMenu.OMO_SHOWDETAILS);
		
		Item mi_showdetails = subMenu1.add(0, KukkerApp.OptionsMenu.OMO_SHOWDETAILS, "Show event details",KukkerApp.Icons.OPTIONS_EVENT_SHOWDETAILS);
		mi_showdetails.setNumericShortcut('1');
				
		Item mi_new = subMenu1.add(0, KukkerApp.OptionsMenu.OMO_CREATEEVENT, "Create new ",KukkerApp.Icons.OPTIONS_EVENT_CREATENEW);
		mi_new.setNumericShortcut('2');
		
		Item mi_forward = subMenu1.add(0, KukkerApp.OptionsMenu.OMO_SENDTOFRIENDS, "Send to a friend",KukkerApp.Icons.OPTIONS_EVENT_SENDTOFRIENDS);
		mi_forward.setNumericShortcut('3');
				
		Item mi_remove = subMenu1.add(0, KukkerApp.OptionsMenu.OMO_DELETEFROMLOCAL, "Delete",KukkerApp.Icons.OPTIONS_EVENT_DELETE);
		mi_remove.setNumericShortcut('0');
	
		SubMenu subMenu2 = menu.addSubMenu(0, 0, "Search",KukkerApp.Icons.OPTIONS_SEARCH);
		subMenu2.add(0, KukkerApp.OptionsMenu.OMO_SEARCHPUBLICEVENTS, "Public events",KukkerApp.Icons.OPTIONS_SEARCH_PUBLIC);
		subMenu2.add(0, KukkerApp.OptionsMenu.OMO_SEARCHSAVEDEVENTS, "Saved events",KukkerApp.Icons.OPTIONS_SEARCH_LOCAL);

		SubMenu subMenu3 = menu.addSubMenu(0, 0, "Help",KukkerApp.Icons.OPTIONS_HELP);
		subMenu3.add(0, 0, "About",KukkerApp.Icons.OPTIONS_HELP_ABOUT);
		subMenu3.add(0, 0, "Tips",KukkerApp.Icons.OPTIONS_HELP_TIPS);

		SubMenu subMenu4 = menu.addSubMenu(0, 0, "Options",KukkerApp.Icons.OPTIONS_OPTIONS);
		subMenu4.add(0, 0, "Preferences",KukkerApp.Icons.OPTIONS_PREFERENCES);

	}
	
	
	
}